android创建快捷方式来打开应用中特定的Activity

您所在的位置:网站首页 安卓创建快捷方式app 能打开原生设置吗 android创建快捷方式来打开应用中特定的Activity

android创建快捷方式来打开应用中特定的Activity

2024-07-11 20:13| 来源: 网络整理| 查看: 265

有段时间没有记录过了。

这也是换工作以后写的第一篇吧,现在公司是在做sdk的接入,对于我来说,能接触到更多不同于以前应用的很多东西,提供自己的服务给更多的人。

虽然是只是进行迭代,但是我觉得对我也有很大的帮助。

现在sdk应设计的要求要在使用了我们sdk的游戏在安装打开以后就要创建一个快捷方式,然后这个快捷方式不是用来打开应用,是为了进行静默下载所创建的,但是我们知道android里边快捷方式是与应用绑定的,他只能打开Activity,我一开始的设想是我捕捉到快捷方式的点击事件,然后开一个service来进行下载,但是首先我们不能捕捉到快捷方式的点击事件,应用这个都是launcher来进行操作的,看过launcher.java的人应该知道他其实是一个Activity,我们打开手机看到的桌面,所有的应用图标都是运行在这个activity上的,快捷方式其实就是一个TextView,然后添加了一个点击事件,点击了以后也没有任何的广播等等的通知,谁也不知道他点击了哪个,所以这是行不同的。

那么就只能用一个没有标题栏的纯粹透明的activity来实现,在点击快捷方式的时候打开这个Activity,然后开启下载任务,然后finish();

这个应该就可以实现了,但是如果你现在当前应用是打开的,那么你点击快捷方式的话是会跳转到你希望的Activity,但是还会呼起程序的第一个Activity。

代码是这样的

(1)

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setResult(RESULT_CANCELED); preferences = getSharedPreferences("cpf", MODE_PRIVATE); setContentView(R.layout.main); int flag = preferences.getInt("flag",0); if (flag == 0) { ShortCutClass.createShortcut(this); preferences.edit().putInt("flag", 1).commit(); }else { Log.e("tag", "我是一只小小小小鸟"); } } package com.skywang.widget; import android.annotation.SuppressLint; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.os.Bundle; import android.os.Parcelable; @SuppressLint("NewApi") public class ShortCutClass { public static void createShortcut(Activity context) { /* Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getString(R.string.app_name)); shortcut.putExtra("duplicate", false);//设置是否重复创建 Intent intent = new Intent(); intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.setClass(context, ExampleAppWidgetProvider.class);//设置第一个页面 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent); ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_launcher); shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes); context.sendBroadcast(shortcut); */ /* Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); //快捷方式的名称 shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getString(R.string.app_name)); shortcut.putExtra("duplicate", false); //不允许重复创建 //指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer //注意: ComponentName的第二个参数必须加上点号(.),否则快捷方式无法启动相应程序 Intent intent = new Intent(context,CopyOfMainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent); //快捷方式的图标 ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_launcher); shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes); context.sendBroadcast(shortcut); */ Intent intent = new Intent(/*"cn.kuwo.player.action.SHORTCUT"*/); Bundle bundle = new Bundle(); bundle.putString("noteId", "100");//传递参数 intent.putExtras(bundle); ComponentName cn = new ComponentName("com.skywang.widget", "com.skywang.widget.CopyOfMainActivity"); intent.setComponent(cn); intent.setAction("android.intent.action.MAIN"); // 自定义action Intent shortcutintent = new Intent( "com.android.launcher.action.INSTALL_SHORTCUT"); // 不允许重复创建 shortcutintent.putExtra("duplicate", false); // 需要现实的名称 shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "apk"); // 快捷图片 Parcelable icon = Intent.ShortcutIconResource.fromContext( context.getApplicationContext(), R.drawable.ic_launcher); shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); // 点击快捷图片,运行的程序主入口 shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent); // 发送广播。OK context.sendBroadcast(shortcutintent); } } 清单文件中设置

可以复制代码进行下测试,这样如果应用没有关掉每次都会呼起来,点击后退键就能看到。

为啥会这个样子了,我查了很多资料,关于创建快捷方式,关于launcher启动,清单文件配置的很多文章,经过很多测试以后,发现原来是应为他们处在同一个Task里边,俩个Activity在同一个Task里边,如果应用没有关闭掉的话那么在我调起我们要打开的Activity的时候,会把这个Task也拿进来,而应用的第一个Activity还在,点击后退就会回到应用界面。所有只要我们在启动模式里边配置下就ok。

android:launchMode="singleInstance"

Demo里边是我试验widget实现快捷方式的代码,对于程序没有影响。

Demo下载地址:http://download.csdn.net/detail/u012808234/9361369



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3